package cn.chf.cloud.controller;

import cn.chf.cloud.entities.PayDTO;
import cn.chf.cloud.resp.ResultData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

/**
 * @author chenhf
 * @date 2025-03-21 16:30
 */
@RestController
@Slf4j
@Tag(name="订单微服务模块",description = "订单服务")
public class OrderController {
    public static final String PaymentSrv_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    /**
     * 一般情况下，通过浏览器的地址栏输入url，发送的只能是get请求
     * 我们底层调用的是post方法，模拟消费者发送get请求，客户端消费者
     * 参数可以不添加@RequestBody
     * @param payDTO
     * @return
     */
    @GetMapping(value = "/consumer/pay/add")
    @Operation(summary = "添加订单")
    public ResultData addOrder(PayDTO payDTO){
        return restTemplate.postForObject(PaymentSrv_URL + "/pay/add",payDTO,ResultData.class);
    }
    @GetMapping(value = "/consumer/pay/update")
    @Operation(summary = "修改订单15")
    public ResultData updateOrder(PayDTO payDTO){
        return restTemplate.exchange(
                PaymentSrv_URL + "/pay/update",HttpMethod.PUT,new HttpEntity<>(payDTO),ResultData.class).getBody();
    }

    // 删除+修改操作作为家庭作业，O(∩_∩)O。。。。。。。
    @GetMapping(value = "/consumer/pay/delete/{id}")
    @Operation(summary = "删除订单")
    public ResultData deleteOrder(@PathVariable("id") Integer id){
        return restTemplate.exchange(PaymentSrv_URL + "/pay/del/"+id,HttpMethod.DELETE,null,ResultData.class,id).getBody();
    }


    @GetMapping(value = "/consumer/pay/get/{id}")
    @Operation(summary = "查询订单")
    public ResultData getPayInfo(@PathVariable("id") Integer id){
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/get/"+id, ResultData.class, id);
    }
    @GetMapping(value = "/consumer/pay/getAll")
    @Operation(summary = "查询所有订单")
    public ResultData getAllPayInfo(){
        return restTemplate.getForObject(PaymentSrv_URL + "/pay/getAll", ResultData.class);
    }

}
